package hys.videoplayerdemo.video_sdk.video;

import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import hys.videoplayerdemo.video_sdk.widget.CustomVideoView;
import hys.videoplayerdemo.video_sdk.widget.CustomVideoView.VideoPlayerListenner;

/**
 * @author: Hys
 * @function: 业务逻辑层
 * @date: 2017/10/15
 */
public class VideoAdSlot implements VideoPlayerListenner {

    private Context mContext;
    /**
     * UI
     */
    private CustomVideoView mVideoView;
    private ViewGroup mParentView;
    /**
     * Data
     */
    private String videoUrl;
    private AdSDKSlotListener mSlotListener;

    public void setmSlotListener(AdSDKSlotListener mSlotListener) {
        this.mSlotListener = mSlotListener;
    }

    private boolean canPause = false; //是否可自动暂停标志位

    public VideoAdSlot( AdSDKSlotListener slotLitener) {
        mSlotListener = slotLitener;
        Log.i("mParentView", "VideoAdSlot: "+0);
        mParentView = slotLitener.getAdParent();
        videoUrl= slotLitener.getUrlVideo();
        Log.i("mParentView", "VideoAdSlot: "+1);
        mContext = mParentView.getContext();
        initVideoView();
    }

    private void initVideoView() {
        mVideoView = new CustomVideoView(mContext, mParentView);
        if (videoUrl != null) {
            mVideoView.setDataSource(videoUrl);
            mVideoView.setListener(this);
        }
        RelativeLayout paddingView = new RelativeLayout(mContext);
        paddingView.setBackgroundColor(mContext.getResources().getColor(android.R.color.black));
        paddingView.setLayoutParams(mVideoView.getLayoutParams());
        mParentView.addView(paddingView);
        mParentView.addView(mVideoView);
    }



    /**
     * 实现play层接口
     */
//    继续播放的接口回调
    @Override
    public void onClickPlay() {
        Log.i("VideoSlot", "onClickPlay: 继续播放");
    }

    @Override
    public void onAdVideoLoadSuccess() {
        if (mSlotListener != null) {
            mSlotListener.onAdVideoLoadSuccess();
        }
    }

    @Override
    public void onAdVideoLoadFailed() {
        if (mSlotListener != null) {
            mSlotListener.onAdVideoLoadFailed();
        }
        //加载失败全部回到初始状态
        canPause = false;
//        git text
    }

    @Override
    public void onAdVideoLoadComplete() {
        if (mSlotListener != null) {
            mSlotListener.onAdVideoLoadComplete();
        }
        mVideoView.setIsRealPause(true);
    }

    @Override
    public void onBufferUpdate(int time) {

    }
//视频暂停的接口回调
    @Override
    public void onClickPause() {
        Log.i("VideoSlot", "onClickPause: 暂停视频");
    }


    //传递消息到appcontext层
    public interface AdSDKSlotListener {
        public String getUrlVideo();

        public ViewGroup getAdParent();

        public void onAdVideoLoadSuccess();

        public void onAdVideoLoadFailed();

        public void onAdVideoLoadComplete();
    }

}
